/**
 * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd
 * author: Benjo Lei <benjo.lei@rock-chips.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "wifi_setting_interface.h"

#include <stdio.h>
#include <string.h>

#include "common.h"
#include "parameter.h"
#include "menu_commond.h"
#include "wifi_management.h"
#include "wlan_service_client.h"
#include "fs_manage/fs_storage.h"

static int format_finish;

#define STR_ARGSETTING          "CMD_ARGSETTING"

int setting_get_cmd_resolve(int nfp, char *buffer)
{
	char buff[1024] = {0};
	int arg;
	time_t t;
	struct tm *tm;
	char tbuf[128];

	strcat(buff, "CMD_ACK_GET_ARGSETTING");
	strcat(buff, "Videolength:");
	arg = parameter_get_recodetime();
	if (arg == 60)
		strcat(buff, "1min");
	else if (arg == 180)
		strcat(buff, "3min");
	else if (arg == 300)
		strcat(buff, "5min");

	strcat(buff, "RecordMode:");
	arg = parameter_get_abmode();
	if (arg == 0)
		strcat(buff, "Front");
	else if (arg == 1)
		strcat(buff, "Rear");
	else if (arg == 2)
		strcat(buff, "Double");

	strcat(buff, "3DNR:");
	if (parameter_get_video_3dnr())
		strcat(buff, "ON");
	else
		strcat(buff, "OFF");

	strcat(buff, "ADAS_LDW:");

	arg = parameter_get_video_ldw();
	if (arg == 1)
		strcat(buff, "ON");
	else
		strcat(buff, "OFF");

	strcat(buff, "WhiteBalance:");
	arg = parameter_get_wb();
	if (arg == 0)
		strcat(buff, "auto");
	else if (arg == 1)
		strcat(buff, "Daylight");
	else if (arg == 2)
		strcat(buff, "fluocrescence");
	else if (arg == 3)
		strcat(buff, "cloudysky");
	else if (arg == 4)
		strcat(buff, "tungsten");

	strcat(buff, "Exposure:");
	arg = parameter_get_ex();
	if (arg == 0)
		strcat(buff, "-3");
	else if (arg == 1)
		strcat(buff, "-2");
	else if (arg == 2)
		strcat(buff, "-1");
	else if (arg == 3)
		strcat(buff, "0");
	else if (arg == 4)
		strcat(buff, "1");

	strcat(buff, "MotionDetection:");
	arg = parameter_get_video_de();
	if (arg == 1)
		strcat(buff, "ON");
	else if (arg == 0)
		strcat(buff, "OFF");

	strcat(buff, "DataStamp:");
	arg = parameter_get_video_mark();
	if (arg == 1)
		strcat(buff, "ON");
	else if (arg == 0)
		strcat(buff, "OFF");

	strcat(buff, "RecordAudio:");
	arg = parameter_get_video_audio();
	if (arg == 1)
		strcat(buff, "ON");
	else if (arg == 0)
		strcat(buff, "OFF");

	strcat(buff, "BootRecord:");
	arg = parameter_get_video_autorec();
	if (arg == 1)
		strcat(buff, "ON");
	else if (arg == 0)
		strcat(buff, "OFF");

	strcat(buff, "Language:");
	arg = parameter_get_video_lan();
	if (arg == 0)
		strcat(buff, "English");
	else if (arg == 1)
		strcat(buff, "Chinese");

	strcat(buff, "Frequency:");
	arg = parameter_get_video_fre();
	if (arg == CAMARE_FREQ_50HZ)
		strcat(buff, "50HZ");
	else if (arg == CAMARE_FREQ_60HZ)
		strcat(buff, "60HZ");

	strcat(buff, "Bright:");
	arg = parameter_get_video_backlt();
	if (arg == LCD_BACKLT_L)
		strcat(buff, "low");
	else if (arg == LCD_BACKLT_M)
		strcat(buff, "middle");
	else if (arg == LCD_BACKLT_H)
		strcat(buff, "hight");

	strcat(buff, "USBMODE:");
	arg = parameter_get_video_usb();
	if (arg == 0)
		strcat(buff, "msc");
	else if (arg == 1)
		strcat(buff, "adb");

	time(&t);
	tm = localtime(&t);
	sprintf(tbuf, "Time:%04d-%02d-%02d %02d:%02d:%02d",
		tm->tm_year + 1900,
		tm->tm_mon + 1,
		tm->tm_mday,
		tm->tm_hour,
		tm->tm_min,
		tm->tm_sec);
	strcat(buff, tbuf);

	strcat(buff, "Version:");
	strcat(buff, parameter_get_firmware_version());

	printf("cmd = %s\n", buff);
	if (0 > write(nfp, buff, strlen(buff)))
		printf("write fail!\r\n");

	return 0;
}

int tcp_func_get_on_off_recording(int nfp, char *buffer)
{
	printf("CMD_GET_Control_Recording\n");

	if (get_src_state() == 0) {
		if (0 > write(nfp, "CMD_ACK_GET_Control_Recording_IDLE",
			      sizeof("CMD_ACK_GET_Control_Recording_IDLE")))
			printf("write fail!\r\n");
	} else {
		if (0 > write(nfp, "CMD_ACK_GET_Control_Recording_BUSY",
			      sizeof("CMD_ACK_GET_Control_Recording_BUSY")))
			printf("write fail!\r\n");
	}
	return 0;
}

int tcp_func_get_ssid_pw(int nfp, char *buffer)
{
	char wifi_info[100] = "CMD_WIFI_INFO";
	char ssid[33], passwd[65];

	parameter_getwifiinfo(ssid, passwd);

	strcat(wifi_info, "WIFINAME:");
	strcat(wifi_info, ssid);
	strcat(wifi_info, "WIFIPASSWORD:");
	strcat(wifi_info, passwd);
	strcat(wifi_info, "MODE:");
	if (parameter_get_wifi_mode() == 0)
		strcat(wifi_info, "AP");
	else
		strcat(wifi_info, "STA");
	strcat(wifi_info, "END");
	if (0 > write(nfp, wifi_info, strlen(wifi_info)))
		printf("write fail!\r\n");

	return 0;
}

int tcp_func_get_format_status(int nfp, char *buffer)
{
	if (get_format_status() == 0) {
		if (0 > write(nfp, "CMD_GET_ACK_FORMAT_STATUS_IDLE",
			      sizeof("CMD_GET_ACK_FORMAT_STATUS_IDLE")))
			printf("write fail!\r\n");
	} else {
		if (get_format_status() == 0) {
			if (0 > write(nfp, "CMD_GET_ACK_FORMAT_STATUS_BUSY",
				      sizeof("CMD_GET_ACK_FORMAT_STATUS_BUSY")))
				printf("write fail!\r\n");
		}
	}
	return 0;
}

static int setting_func_video_length(int nfp, char *str)
{
	return setting_func_video_length_ui(str);
}

static int setting_func_record_mode(int nfp, char *str)
{
	return setting_func_car_ui(str);
}

static int setting_func_3dnr(int nfp, char *str)
{
	return setting_func_3dnr_ui(str);
}

static int setting_func_adas_ldw(int nfp, char *str)
{
	return setting_func_ldw_ui(str);
}

static int setting_func_white_balance(int nfd, char *str)
{
	return setting_func_white_balance_ui(str);
}

static int setting_func_exposure(int nfd, char *str)
{
	return setting_func_exposure_ui(str);
}

static int setting_func_data_stamp(int nfd, char *str)
{
	return setting_func_data_stamp_ui(str);
}

static int setting_func_record_audio(int nfd, char *str)
{
	return setting_func_record_audio_ui(str);
}

static int setting_func_boot_record(int nfd, char *str)
{
	return setting_func_record_audio_ui(str);
}

static int setting_func_language(int nfd, char *str)
{
	return setting_func_language_ui(str);
}

static int setting_func_frequency(int nfd, char *str)
{
	return setting_func_frequency_ui(str);
}

static int setting_func_bright(int nfd, char *str)
{
	return setting_func_backlight_lcd_ui(str);
}

static int setting_func_USBMODE(int nfd, char *str)
{
	return setting_func_USBMODE_ui(str);
}

static void setting_func_format_finish(void)
{
	printf("setting_func_format_finish\n");
	format_finish = 1;
}

static int setting_func_format(int nfd, char *str)
{
	format_finish = 0;
	setting_func_format_ui(&setting_func_format_finish);

	while (1) {
		if (format_finish != 0)
			break;
		/* Wait for formatting to complete */
		sleep(1);
	}
	if (-1 == write(nfd, "CMD_GET_ACK_FORMAT_STATUS_FINISH",
			sizeof("CMD_GET_ACK_FORMAT_STATUS_FINISH")))
		printf("write fail!\r\n");
	return 0;
}

static int setting_func_dateset(int nfd, char *str)
{
	return setting_func_setdata_ui(str);
}

static int setting_func_recovery(int nfd, char *str)
{
	return setting_func_recovery_ui();
}

const struct type_cmd_func cmd_setting_tab[] = {
	{setting_func_video_length,	"Videolength",},
	{setting_func_record_mode,	"RecordMode",},
	{setting_func_3dnr,		    "3DNR",},
	{setting_func_adas_ldw,		"ADAS_LDW",},
	{setting_func_white_balance, "WhiteBalance",},
	{setting_func_exposure,		"Exposure",},
	{setting_func_data_stamp,	"DataStamp",},
	{setting_func_record_audio,	"RecordAudio",},
	{setting_func_boot_record,	"BootRecord",},
	{setting_func_language,		"Language",},
	{setting_func_frequency,	"Frequency",},
	{setting_func_bright,		"Bright",},
	{setting_func_USBMODE,		"USBMODE",},
	{setting_func_format,		"Format",},
	{setting_func_dateset,		"DateSet",},
	{setting_func_recovery,		"Recovery",},
};

int setting_cmd_resolve(int nfd, char *cmdstr)
{
	int size = sizeof(cmd_setting_tab) / sizeof(cmd_setting_tab[0]);
	int i = 0;
	char *arg;

	for (i = 0; i < size; i++) {
		if (strstr(cmdstr, cmd_setting_tab[i].cmd) != 0 &&
		    cmd_setting_tab[i].func != NULL) {
			arg = &cmdstr[sizeof(STR_ARGSETTING) +
				      strlen(cmd_setting_tab[i].cmd)];
			printf("setting buffer = %s\n arg = %s\n",
			       cmdstr, arg);
			return cmd_setting_tab[i].func(nfd, arg);
		}
	}

	return -1;
}
